package com.mjf.app;

import com.mjf.bean.Bean1;
import com.mjf.bean.Bean2;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * Flink 双流 join 测试
 */
public class FlinkDateStreamJoin {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 读取两个端口数据创建流并提取时间戳生成 watermark
        SingleOutputStreamOperator<Bean1> stream1 = env.socketTextStream("hadoop103", 8888)
                .map(line -> {
                            String[] cols = line.split(",");
                            return new Bean1(cols[0], cols[1], Long.parseLong(cols[2]));
                        }
                ).assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Bean1>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<Bean1>() {
                                    @Override
                                    public long extractTimestamp(Bean1 element, long recordTimestamp) {
                                        return element.getTs() * 1000L;
                                    }
                                })
                );

        SingleOutputStreamOperator<Bean2> stream2 = env.socketTextStream("hadoop103", 9999)
                .map(line -> {
                            String[] cols = line.split(",");
                            return new Bean2(cols[0], cols[1], Long.parseLong(cols[2]));
                        }
                ).assignTimestampsAndWatermarks(
                        WatermarkStrategy.<Bean2>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<Bean2>() {
                                    @Override
                                    public long extractTimestamp(Bean2 element, long recordTimestamp) {
                                        return element.getTs() * 1000L;
                                    }
                                })
                );

        // 双流 Join
        SingleOutputStreamOperator<Tuple2<Bean1, Bean2>> joinDS =
                stream1.keyBy(Bean1::getId)
                .intervalJoin(stream2.keyBy(Bean2::getId))
                .between(Time.seconds(-5), Time.seconds(5))
//                .lowerBoundExclusive()  // 排除下边界
//                .upperBoundExclusive()  // 排除上边界
                .process(new ProcessJoinFunction<Bean1, Bean2, Tuple2<Bean1, Bean2>>() {
                    @Override
                    public void processElement(Bean1 left, Bean2 right, ProcessJoinFunction<Bean1, Bean2, Tuple2<Bean1, Bean2>>.Context ctx, Collector<Tuple2<Bean1, Bean2>> out) throws Exception {
                        out.collect(new Tuple2<Bean1, Bean2>(left, right));
                    }
                });

        joinDS.print();

        env.execute(FlinkDateStreamJoin.class.getName());

    }
}
